import { prisma } from "@/lib/prisma"
import { SalesStatisticsHeader } from "@/components/sales/sales-statistics-header"
import { SalesStatisticsList } from "@/components/sales/sales-statistics-list"
import { Metadata } from "next"

export const metadata: Metadata = {
  title: "销售统计 - 商场销售系统",
  description: "销售统计页面",
}

export default async function SalesStatisticsPage() {
  const sales = await prisma.sale.findMany({
    include: {
      items: {
        include: {
          product: true
        }
      }
    },
    orderBy: {
      createdAt: 'desc'
    }
  })

  // 统计销售数据
  const totalSales = sales.length;
  const totalRevenue = sales.reduce((sum, sale) => sum + parseFloat(sale.totalAmount.toString()), 0);
  const totalItemsSold = sales.reduce((sum, sale) => sum + sale.items.reduce((itemSum, item) => itemSum + item.quantity, 0), 0);

  return (
    <div className="space-y-4 p-8">
      <SalesStatisticsHeader />
      <SalesStatisticsList 
        totalSales={totalSales} 
        totalRevenue={totalRevenue} 
        totalItemsSold={totalItemsSold} 
      />
    </div>
  )
} 